# -*- coding: utf-8 -*-
# @Time    : 2025/08/20 08:40
# @Author  : Mr.su
# @FileName: methods.py
# @FileDesc: 
import json
import hashlib, random


def create_md5(data):
    # 生成md5方法
    if isinstance(data, str): data = data.encode("utf-8")
    m = hashlib.md5()
    m.update(data)
    return m.hexdigest()

# noinspection PyMethodParameters
class ProcessJson:
    """封装json数据获取方法"""
    url = []

    @classmethod
    def parse_data(self, data, rules, rule_count, index):
        if data:
            if rules[index] != "[]":
                # 判断当前层是否为字典
                if isinstance(data, dict):
                    data = data[rules[index]]
                    # 如果当前规则索引加1不等于规则数量则当前规则索引不是最后一层
                    if index + 1 != rule_count:
                        self.parse_data(data, rules, rule_count, index + 1)
                    # 若当前规则索引加1等于规则数量
                    elif index + 1 == rule_count:
                        self.url.append(data)

            if rules[index] == "[]":
                # 判断当前层是否为列表
                if isinstance(data, list):
                    for record in data:
                        self.parse_data(record, rules, rule_count, index + 1)

    @classmethod
    def parse_rule(self, data, rule_str):
        # 将rule规则以.分割成列表
        self.url = []
        rules = rule_str.split(".")
        self.parse_data(data, rules, len(rules), 0)
        return self.url
